package com.ch.engine.biz.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.ch.engine.biz.manager.AddressManager;
import com.ch.engine.biz.service.BizCustomerService;
import com.ch.engine.modules.entity.Customer;
import com.ch.engine.modules.service.ICustomerService;
import com.ch.engine.utils.DateUtil;
import com.ch.engine.utils.VcExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * 客户service
 *
 * @author wangchunhui
 * @date 2024/9/4 9:33
 */
@Service
@RequiredArgsConstructor
public class BizCustomerServiceImpl implements BizCustomerService {

    private final AddressManager addressManager;
    private final ICustomerService customerService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void testSaveBatch() {
        try {
            addressManager.errorInvoker();
        } catch (Exception e) {
            e.printStackTrace();
        }
        customerService.save(new Customer("小王"));
    }

    @Override
    public void writeExcel(HttpServletResponse response) {
        try {
            String repaymentPlan = "/excel/repayment_plan.xlsx";
            String suffix = ".xlsx";
            String fileName = "维信通还款计划生成表" + LocalDateTimeUtil.format(LocalDateTime.now(), DateTimeFormatter.ofPattern(DateUtil.YYYYMMDDHHMMSS)) + suffix;
            InputStream inputStream = new ClassPathResource(repaymentPlan).getInputStream();
            Map<String, String> map = new HashMap<>();
            map.put("position13","我是");
            map.put("error6","大王");
            VcExcelUtil.downloadExcel(inputStream, map, fileName, response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
